<?php
define('DB_NAMA', 'sipkandb_dev');
define('DB_USER', 'root');
define('DB_PASSWORD', 'bismillah');
define('DB_HOST', 'localhost');

function koneksi_buka() {
	mysql_select_db(DB_NAMA,mysql_connect(DB_HOST,DB_USER,DB_PASSWORD));
}

function koneksi_tutup() {
	mysql_close(mysql_connect(DB_HOST,DB_USER,DB_PASSWORD));
}

function sms_baca() {
	$qsms=mysql_query("
	SELECT a.kd_sms, a.kd_perkara, b.isi_pesan, c.no_hp FROM t_sms a
	LEFT JOIN t_sms_pesan b ON a.kd_pesan = b.kd_pesan
	LEFT JOIN t_pegawai c ON a.kd_pegawai = c.kd_pegawai
	WHERE kirim=0");
	while($sms=mysql_fetch_array($qsms)) {
		sms_kirim($sms[1],$sms[2],$sms[3]);
		sms_terbaca($sms[0]);
	}
}

function sms_kirim($perkara,$pesan,$no) {
	$pesan = str_replace("[kd_perkara]",$perkara,$pesan);
	$panjang = strlen($pesan);
	if($panjang < 160) { 
		shell_exec('gammu sendsms TEXT '.$no.' -text "'.$pesan.'"');
		$jumlah = 1;
	} else {
		shell_exec('gammu sendsms TEXT '.$no.' -len '.$panjang.' -text "'.$pesan.'"');
		$jumlah = round($panjang/160)+1;
	}
}

function sms_terbaca($id) {
	mysql_query("UPDATE t_sms SET kirim=1 WHERE kd_sms=".$id);
}

koneksi_buka();
sms_baca();
koneksi_tutup();
?>
